<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sets a client-side timeout for this query</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongocursor.tailable.html">« MongoCursor::tailable</a></li>
      <li style="float: right;"><a href="mongocursor.valid.html">MongoCursor::valid »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.mongocursor.html">MongoCursor</a></li>
    <li>Sets a client-side timeout for this query</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongocursor.timeout" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoCursor::timeout</h1>
  <p class="verinfo">(PECL mongo &gt;=1.0.3)</p><p class="refpurpose"><span class="refname">MongoCursor::timeout</span> &mdash; <span class="dc-title">Sets a client-side timeout for this query</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongocursor.timeout-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>MongoCursor::timeout</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$ms</code></span>): <span class="type"><a href="class.mongocursor.html" class="type MongoCursor">MongoCursor</a></span></div>

  <p class="para rdfs-comment">
   A timeout can be set at any time and will affect subsequent queries on the 
   cursor, including fetching more results from the database.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongocursor.timeout-parameters">
  <h3 class="title">Parameters</h3>
  <dl>

   
    <dt>

     <code class="parameter">ms</code>
    </dt>

    <dd>

     <p class="para">
      The number of milliseconds for the cursor to wait for a response. Use
      <code class="literal">-1</code> to wait forever. By default, the cursor will wait
      <var class="varname">30000</var> milliseconds (30 seconds).
     </p>
    </dd>

      
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongocursor.timeout-returnvalues">
  <h3 class="title">Return Values</h3>  
  <p class="para">
   This cursor.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-mongocursor.timeout-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   Causes methods that fetch results to throw a
   <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> if the query takes longer
   than the specified number of milliseconds.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongocursor.timeout-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-156">
   <p><strong>Example #1 <span class="function"><strong>MongoCursor::timeout()</strong></span> example</strong></p>
   <div class="example-contents"><p>
    In the following example, the driver will wait forever for the initial
    database response, and then wait 100ms for subsequent responses.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">timeout</span><span style="color: #007700">(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;$cursor-&gt;hasNext()&nbsp;executes&nbsp;the&nbsp;query.&nbsp;An&nbsp;infinite&nbsp;timeout&nbsp;has&nbsp;been&nbsp;set,&nbsp;so<br />&nbsp;*&nbsp;the&nbsp;driver&nbsp;will&nbsp;wait&nbsp;as&nbsp;long&nbsp;as&nbsp;necessary&nbsp;for&nbsp;a&nbsp;response.<br />&nbsp;*/<br /></span><span style="color: #007700">while&nbsp;(</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hasNext</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">timeout</span><span style="color: #007700">(</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;A&nbsp;timeout&nbsp;has&nbsp;now&nbsp;been&nbsp;set,&nbsp;so&nbsp;if&nbsp;the&nbsp;cursor&nbsp;needs&nbsp;to&nbsp;get&nbsp;more&nbsp;results<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;from&nbsp;the&nbsp;database,&nbsp;it&nbsp;will&nbsp;only&nbsp;wait&nbsp;100ms&nbsp;for&nbsp;a&nbsp;response.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getNext</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">MongoCursorTimeoutException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"query&nbsp;took&nbsp;too&nbsp;long!"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 notes" id="refsect1-mongocursor.timeout-notes">
  <h3 class="title">Notes</h3>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
     This does not cause the MongoDB server to cancel long-running operations;
     it only instructs the driver to stop waiting for a response and throw a
     <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> after a set time.
     If you need to specify a server-side timeout for a query, consider using
     <span class="methodname"><a href="mongocursor.maxtimems.html" class="methodname">MongoCursor::maxTimeMS()</a></span>.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mongocursor.timeout-seealso">
  <h3 class="title">See Also</h3>
  <ul class="simplelist">
   <li class="member"><span class="methodname"><a href="mongocursorinterface.timeout.html" class="methodname" rel="rdfs-seeAlso">MongoCursorInterface::timeout()</a> - Sets a client-side timeout for this query</span></li>
   <li class="member">The <code class="literal">socketTimeoutMS</code> option for <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span></li>
  </ul>
 </div>


</div></div></div></body></html>